"""
Case Type   : charset
Case Name   : 使用character set set设置字符编码类型，部分合理报错
Create At   : 2024.09.23
Owner       : @zhanghuan96
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、使用character set set设置字符编码类型
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、部分报错
    4、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_char_0002'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = (f"drop database if exists {self.db_name};"
                   f"create database {self.db_name}  dbcompatibility ='B';")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(1,
                         sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                         '兼容B库创建失败')
        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '---step2:使用charset设置字符编码类型;expect:部分报错---'
        self.log.info(text)
        sql_cmd2 = ("set character set gbk;"
                    "show client_encoding;")
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        self.assertIn('SET', sql_res2, '创建失败')
        self.assertEqual('GBK', sql_res2.splitlines()[-2].strip(),
                         '创建失败')

        sql_cmd = ("set character set 'EUC_CN';"
                   "show client_encoding;")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('SET', sql_res, '创建失败')
        self.assertEqual('EUC_CN', sql_res.splitlines()[-2].strip(),
                         '创建失败')

        sql_cmd3 = ('set character set "utf8";'
                    'show client_encoding;')
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3,
                                                 dbname=self.db_name)
        self.log.info(sql_res3)
        self.assertIn('SET', sql_res3, '创建失败')
        self.assertEqual('UTF8', sql_res3.splitlines()[-2].strip(),
                         '创建失败')

        sql_cmd4 = "set character set aaa;"
        self.log.info(sql_cmd3)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4,
                                                 dbname=self.db_name)
        self.log.info(sql_res4)
        self.assertIn('ERROR:  invalid value for parameter '
                      '"client_encoding"', sql_res4, '创建失败')

        sql_cmd5 = "set charset;"
        self.log.info(sql_cmd5)
        sql_res5 = self.sh_primary.execut_db_sql(sql_cmd5,
                                                 dbname=self.db_name)
        self.log.info(sql_res5)
        self.assertIn('ERROR:  syntax error', sql_res5, '创建失败')

        sql_cmd6 = ("set character set default;"
                    "show client_encoding;")
        self.log.info(sql_cmd6)
        sql_res6 = self.sh_primary.execut_db_sql(sql_cmd6,
                                                 dbname=self.db_name)
        self.log.info(sql_res6)
        self.assertIn('SET', sql_res6, '创建失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd7 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd7)
        sql_res7 = self.sh_primary.execut_db_sql(sql_cmd7, dbname='postgres')
        self.log.info(sql_res7)
        self.assertEqual(1,
                         sql_res7.count(self.constant.DROP_DATABASE_SUCCESS),
                         '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
